<?php

namespace App\Http\Controllers\V1;

use App\Http\Controllers\ApiBaseController;
use App\Models\BalanceLog;
use App\Models\CustomerProduct;
use App\Models\CzOrder;
use App\Models\CzProduct;
use App\Models\CzSpec;
use App\Models\Payment;
use App\Models\SysUser;
use App\Services\CzOrderService;
use EasyWeChat\Factory;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class PaymentController extends ApiBaseController
{
    public function index(Request $request)
    {
        $params = $request->all();
        $paginator = Payment::filter($params)->with('user:id,nickname,username')->orderBy('id', 'desc')->paginate($this->pageSize);
        // 获取支付授权目录

        // // return successPage($paginator);
        // $payPath = "";
        // // $info = MpAccount::where('create_by', $user_id)->find();
        // $url = env('APP_URL');

        return success(['data' => page_format($paginator)]);
    }
    public function add(Request $request)
    {
        $params = $request->all();
        if ($params['is_default']) {
            $old_pay = Payment::where('user_id', $params['user_id'])
                ->where('is_default', 1)->first();
            if ($old_pay) {
                $old_pay->is_default = 0;
                $old_pay->save();
            }
        }
        // $params['user_id'] = $mp_info->id;
        $res = Payment::create($params);
        return success();
    }
    public function show(Request $request)
    {
        $id = request('id');
        $data = Payment::find($id);
        return success(['data' => $data]);
    }
    public function getPayAllList(Request $request)
    {
        // $id = request('id');
        $data = Payment::get();
        return success(['data' => $data]);
    }
    public function change(Request $request)
    {
        // $params = $request->only(['id', 'limit_time', 'limit_type', 'limit_type_value', 'limit_value', 'remark', 'status']);
        // $res = ProductLimit::where('id', $params['id'])->update($params);
        // return success();
        // unset($params['file_path']);
        $params = $request->all();
        $old_pay = Payment::where('user_id', $params['user_id'])->where('is_default', 1)->first();
        if ($old_pay && $old_pay->id != $params['id'] && $params['is_default'] == 1) {
            $old_pay->is_default = 0;
            $old_pay->save();
        }
        unset($params['created_at']);
        unset($params['updated_at']);
        Payment::where('id', $params['id'])->update($params);
        return success([], '更新成功');
    }
    public function destory()
    {
        return error('暂不允许删除');
        $ids = request('ids');
        // $id_arr = explode(',', $ids);/
        if (!is_array($ids)) {
            $id_arr = [$ids];
        } else {
            $id_arr = $ids;
        }
        Payment::destroy($id_arr);
        return success([], '删除成功');
    }
    public function getPayTemplateList(Request $request)
    {
        $list = base_path("payChannel") . "/list.php";
        // $setting_file = app()->getRootPath() . "/payChannel/list.php";
        if (!is_file($list)) {
            return error('配置文件不存在');
        }
        $data = require_once($list);

        return success(['data' => $data['list']]);
    }
    //上传证书文件
    public function uploadPayfile(Request $request)
    {
        // $files = request()->file();
        // $fileSize = 1024 * 5;  //支付文件最大5k
        // $validate = validate([
        //     "file" => 'fileSize:' . $fileSize . '|fileExt:pem'
        // ]);
        // if (!$validate->check($files)) {
        //     return error($validate->getError());
        // }
        // $type = $request['type'];
        // // 生成新的名字

        // if ($type == 'apiclient_key') {
        //     $saveName = \think\facade\Filesystem::disk('payfile')->putFile('apiclient_key', $files['file']);

        //     // $file->storePubliclyAs('uploads/pay/apiclient_key', $newFilename, ['disk' => 'public']);
        // } else if ($type == 'apiclient_cert') {
        //     $saveName = \think\facade\Filesystem::disk('payfile')->putFile('apiclient_cert', $files['file']);
        // } else {
        //     return error('参数错误');
        // }
        // // 支付证书路径返回给前端,无法访问
        // return success($saveName);
    }
}
